/*
 * Funambol is a mobile platform developed by Funambol, Inc. 
 * Copyright (C) 2008 Funambol, Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by
 * the Free Software Foundation with the addition of the following permission 
 * added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
 * WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE 
 * WARRANTY OF NON INFRINGEMENT  OF THIRD PARTY RIGHTS.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Affero General Public License 
 * along with this program; if not, see http://www.gnu.org/licenses or write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301 USA.
 * 
 * You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite 
 * 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License
 * version 3, these Appropriate Legal Notices must retain the display of the
 * "Powered by Funambol" logo. If the display of the logo is not reasonably 
 * feasible for technical reasons, the Appropriate Legal Notices must display
 * the words "Powered by Funambol".
 */

#include "EventChangesTracker.h"

EventChangesTracker::EventChangesTracker(const char* cpath){
    
    path        = cpath;
    newKeys     = NULL;
    updatedKeys = NULL;
    deletedKeys = NULL;
    allKeys     = NULL;
 
}

EventChangesTracker::~EventChangesTracker(){
    if (newKeys)     { delete newKeys;     newKeys     = NULL; }
    if (updatedKeys) { delete updatedKeys; updatedKeys = NULL; }
    if (deletedKeys) { delete deletedKeys; deletedKeys = NULL; }
    if (allKeys)     { delete allKeys;     allKeys     = NULL; }
        
}

/**
* It is used to get the query string to be used to retrieved the desidered items
* type = 1: get the new
* type = 2: get the udpated
* type = 3: get the deleted
*/
StringBuffer EventChangesTracker::getQueryForItems(int type, unsigned long lastSyncTime) {
    
    StringBuffer query;
    query.append(SELECT_EVENT_ID);
    
    // field name
    if(type == 1) {
        query.append(" creation > ");
    } else if (type == 2) {
        query.append(" updated > ");
    } else if (type == 3) {
        query.append(" deleted > ");
    }
    query.append(lastSyncTime);
    
    // condition
    if(type == 1) {
        query.append(" and updated = 0 and deleted = 0");
    } else if (type == 2) {
        query.append(" and deleted = 0");
    } else if (type == 3) {
        query.append(" and creation < ");
        query.append(lastSyncTime);                
    }
    
    // the ordination        
    query.append(" order by event_id;");
    printf("The query modification is %s\n", query.c_str());
    return query;    
}

StringBuffer EventChangesTracker::getQueryForAllItems() {
    
    StringBuffer query;
    query.append(SELECT_ALL_EVENT_ID);           
    printf("The query for all items is %s\n", query.c_str());
    return query;    
}

void EventChangesTracker::getModifications(unsigned long lastSyncTime) {

    SQLiteDriver driver(path);
    ArrayList tmp;
    StringBuffer query;
    
    // build the query for the new items
    query = getQueryForItems(1, lastSyncTime);    
    int ret = driver.queryIndex(query, tmp);
    if (ret != 0){
        //LOG.error("Error in getModifications for new items: %i", ret);
        printf("Error in getModifications for new items: %i", ret);
    }
    if (newKeys){
        delete newKeys; newKeys = NULL;
    }
    newKeys = new ArrayListEnumeration(tmp);

    tmp.clear();
    query.empty();
    
    // build the query for the updated items
    query = getQueryForItems(2, lastSyncTime);   
    ret = driver.queryIndex(query, tmp);
    if (ret != 0){
        //LOG.error("Error in getModifications for updated items: %i", ret);
        printf("Error in getModifications for updated items: %i", ret);
    }
    if (updatedKeys){
        delete updatedKeys; updatedKeys = NULL;
    }
    updatedKeys = new ArrayListEnumeration(tmp);

    tmp.clear();
    query.empty();
    
    // build the query for the deleted items
    query = getQueryForItems(3, lastSyncTime);
    ret = driver.queryIndex(query, tmp);
    if (ret != 0){
        //LOG.error("Error in getModifications for updated items: %i", ret);
        printf("Error in getModifications for deleted items: %i", ret);
    }
    if (deletedKeys){
        delete deletedKeys; deletedKeys = NULL;
    }
    deletedKeys = new ArrayListEnumeration (tmp);
}

void EventChangesTracker::getAll() {
    
    SQLiteDriver driver(path);
    ArrayList tmp;
    StringBuffer query;
    
    // build the query for the new items
    query = getQueryForAllItems();    
    int ret = driver.queryIndex(query, tmp);
    if (ret != 0){
        //LOG.error("Error in getAll for new items: %i", ret);
        printf("Error in getAll for items: %i", ret);
    }
    if (allKeys){
        delete allKeys; allKeys = NULL;
    }
    allKeys = new ArrayListEnumeration(tmp);
   
      
}
